﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace A
{
    public class Valid
    {

        public static bool IsValidMobile(string mobile)
        {
            string charvalid = "+1234567890";
            foreach (char c in mobile)
            {
                if (charvalid.IndexOf(c.ToString().ToUpper()) < 0)
                {
                    return false;
                }
            }
            return true;
        }

        public static bool IsValidNumber(string content)
        {
            string charvalid = "1234567890";
            foreach (char c in content)
            {
                if (charvalid.IndexOf(c.ToString().ToUpper()) < 0)
                {
                    return false;
                }
            }
            return true;
        }

        public static bool IsValidEmail(string email)
        {
            if ( email.IndexOf("@") < 0 || email.IndexOf(".") < 0)
            {
                return false;
            }
            return true;
        }

        public static bool IsValidName(string name)
        {
            name = name.Trim();
            string charvalid = " ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            foreach (char c in name)
            {
                if (charvalid.IndexOf(c.ToString().ToUpper()) < 0)
                {
                    return false;
                }
            }
            return true;
        }

        public static bool ValidPnrNumber(string pnr)
        {
            if (pnr.Length == 0)
                return true;
            if (pnr.Length == 7 && A.Valid.IsValidNumber(pnr))
                return true;
            if (pnr.Length == 6 && !A.Valid.IsValidNumber(pnr))
                return true;
            return false;
        }

        #region Date & Time
        public static bool IsValidDateTimeValue(object value)
        {
            bool temp = false;// not a datetime value
            try { Convert.ToDateTime(value); temp = true; }
            catch { temp = false; }

            return temp;
        }
        #endregion

        #region Number
        public static bool IsValidDecimalValue(object value)
        {
            bool temp = false;// not a decima value
            try
            {
                if (value != DBNull.Value && value != null)
                {
                    Convert.ToDecimal(value);
                    temp = true;
                }
            }
            catch { temp = false; }
            return temp;
        }
        public static bool IsValidIntegerValue(object value)
        {
            bool temp = false;// not a integer value
            try
            {
                if (value != DBNull.Value && value != null)
                {
                    Convert.ToInt64(value);
                    temp = true;
                }
            }
            catch { temp = false; }
            return temp;
        }
        public static bool IsValidByteValue(object value)
        {
            bool temp = false;// not a integer value
            try
            {
                if (value != DBNull.Value && value != null)
                {
                    Convert.ToByte(value);
                    temp = true;
                }
            }
            catch { temp = false; }
            return temp;
        }


        #endregion

        #region IsNull & CheckLength
        public static bool IsNullValue(object value)
        {
            if (value == null || value == DBNull.Value)
                return true;
            return false;
        }

        public static bool IsNullValueOrEmptyString(object value)
        {
            bool isNullOrEmpty = false;
            if (IsNullValue(value))
                isNullOrEmpty = true;
            else
            {
                if (string.IsNullOrEmpty(value.ToString().Trim()))
                    isNullOrEmpty = true;
            }
            return isNullOrEmpty;
        }

        public static string GetContentByMaxLength(string strContent, int maxLength)
        {
            return GetContentByMaxLength(strContent, maxLength, 3);
        }
        public static string GetContentByMaxLength(string strContent, int maxLength, int number_of_dot)
        {
            if (strContent != null)
            {
                if (strContent.Length > maxLength)
                {
                    switch (number_of_dot)
                    {
                        case 0:
                            strContent = strContent.Substring(0, maxLength).Trim();
                            break;
                        case 1:
                            strContent = strContent.Substring(0, maxLength).Trim() + ".";
                            break;
                        case 2:
                            strContent = strContent.Substring(0, maxLength).Trim() + "..";
                            break;
                        case 3:
                            strContent = strContent.Substring(0, maxLength).Trim() + "...";
                            break;
                        default:
                            string arrDot = string.Empty;
                            for (int i = 0; i < number_of_dot; i++)
                                arrDot += ".";
                            strContent = strContent.Substring(0, maxLength).Trim() + arrDot;
                            break;
                    }

                }
                return strContent;
            }
            return string.Empty;
        }
        #endregion
        
    }
}
